package com.ruoyi.framework.aspectj;

import com.ruoyi.eamp.domain.EampCarLogs;
import com.ruoyi.eamp.domain.EampCarUse;
import com.ruoyi.eamp.service.IEampCarListService;
import com.ruoyi.eamp.service.IEampCarLogsService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;


@Aspect
@Component
public class UseApplyAspect {
    @Autowired
    private IEampCarLogsService eampCarLogsService;

    @Autowired
    private IEampCarListService eampCarListService;

    @Pointcut("@annotation(com.ruoyi.common.annotation.UseApply)")
    public void useapply(){

    }

    @Around("useapply()")
    public Object around(ProceedingJoinPoint joinPoint)throws Throwable {
        Long carId=null;
        Long carOwners=null;
        Long useId=null;
        Long useApplicant = null;
        Date date = new Date();
        String name = ((MethodSignature) joinPoint.getSignature()).getMethod().getName();
        Object result = joinPoint.proceed();
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            if(arg instanceof EampCarUse){
               carId = ((EampCarUse) arg).getCarId();
               useId = ((EampCarUse) arg).getUseId();
               carOwners = eampCarListService.selectEampCarListByCarId(carId).getCarOwners();
               useApplicant = ((EampCarUse) arg).getUseApplicant();
            }
        }
        EampCarLogs eampCarLogs = new EampCarLogs(null,1L,carOwners,carId,useApplicant,date,name,useId,1L);
        eampCarLogsService.insertEampCarLogs(eampCarLogs);

        return result;
    }
}
